package com.yuyou.fn.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mq.RedisMessageQueue;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.mapper.SysUserMapper;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.JSONS;
import com.yuyou.fn.common.util.TimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.platform.mapper.BusTeamMapper;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IBusTeamService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * BusTeam 表数据服务层接口实现类
 *
 */
@Service
public class BusTeamServiceImpl extends SuperServiceImpl<BusTeamMapper, BusTeam,Long> implements IBusTeamService {


    @Resource
    private IAreaTeamService areaTeamService ;


    @Override
    public Page<BusTeam> findBusTeamForPage(int pageNo, int pageSize) {
        return findPageByExample(BusTeam.createExample(),pageNo,pageSize );
    }

    @Override
    public void saveOrUpdateBusTeam(Long busTeamId, Long areaTeamId, String name) {

        if( busTeamId == null && areaTeamId == null &&name == null  ){
            throw new IllegalArgumentException("Illegal arguments");
        }

        if(busTeamId == null){
            if(areaTeamId == null ){
                throw new IllegalArgumentException("Require areaTeamId arguments");
            }
            BusTeam busTeam = new BusTeam();
            busTeam.setAreaTeamId(areaTeamId);
            busTeam.setName(name);
            busTeam.setCreateTime(TimeUtils.now());
            busTeam.setUpdateTime(TimeUtils.now());
            setAreaTeamName(busTeam);
            checkBusTeam(busTeam);
            insertSelective(busTeam);

        }else{

            BusTeam busTeam = new BusTeam();
            busTeam.setBusTeamId(busTeamId);
            if(areaTeamId != null ){
                busTeam.setAreaTeamId(areaTeamId);
                setAreaTeamName(busTeam);
            }
            busTeam.setName(name);
            busTeam.setUpdateTime(TimeUtils.now());
            checkBusTeam(busTeam);
            updateSelectiveById(busTeam) ;

        }


    }

    private void checkBusTeam(BusTeam busTeam) {
        Example<BusTeam> example = BusTeam.createExample();
        example.createCriteria().andEqualTo("name",busTeam.getName());
        BusTeam one = findOneByExample(example);
        if(one != null && (busTeam.getBusTeamId() == null || !busTeam.getBusTeamId().equals(one.getBusTeamId()))){
            throw new BusException("业务组名重复");
        }
    }

    @Transactional
    @Override
    public void deleteBusTeam(Long busTeamId) {
        deleteById(busTeamId);
    }

    @Override
    public List<BusTeam> findBusTeamForList(Long areaTeamId,Long busTeamId) {
        Example<BusTeam> example = BusTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,areaTeamId,busTeamId);
        return findListByExample(example);
    }

    private void setAreaTeamName(BusTeam busTeam) {
        if(busTeam.getAreaTeamId() != null ){
            busTeam.setAreaTeamName(areaTeamService.findTeamNameById(busTeam.getAreaTeamId()));
        }
    }

    @Override
    public String findTeamNameById(Long busTeamId) {
        Example<BusTeam> example = BusTeam.createExample();
        example.selectProperties("name");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busTeamId",busTeamId);
        BusTeam busTeam = findOneByExample(example);
        return busTeam == null ? StringUtils.EMPTY : busTeam.getName() ;
    }
}